Skip to content

fix(form-core): clear stale onBlur errors on re-submission#2120

Merged
crutchcorn merged 3 commits into
TanStack:mainfrom
restareaByWeezy:fix/2034-field-level-validation-on-submit
Jul 9, 2026
Merged

fix(form-core): clear stale onBlur errors on re-submission#2120
crutchcorn merged 3 commits into
TanStack:mainfrom
restareaByWeezy:fix/2034-field-level-validation-on-submit

Conversation

@restareaByWeezy

@restareaByWeezy restareaByWeezy commented Apr 11, 2026

Copy link
Copy Markdown
Contributor

Changes

Fixes #2034

When a field has an onBlur validator that produces an error, subsequent submit attempts get blocked. The canSubmit check in _handleSubmit returns early before validateAllFields has a chance to re-run and clear the stale error.

This patch skips the early return on re-submission (submissionAttempts > 1) so that validateAllFields actually runs again. First-submission behavior is unchanged.

Added a regression test that reproduces the exact scenario from the issue — conditional field validation with type switching.

Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes

    • Stale field-level validation errors from prior onBlur validation are now cleared on resubmission; fields are re-validated so subsequent submits reflect current errors.
  • Tests

    • Added a regression test ensuring onBlur validators re-run and stale errors don't block a later successful submit.
  • Chores

    • Prepared a patch release note marking this fix.

…errors

When a field has an onBlur validator that produces an error, subsequent
form submission attempts are blocked by a canSubmit check that runs
before validateAllFields. This means stale onBlur errors (e.g. from a
field that is no longer invalid) prevent re-submission.

Fix: on re-submission attempts (submissionAttempts > 1), skip the early
canSubmit return so validateAllFields can re-run all field validators for
the submit cause. Stale errors are cleared and the isFieldsValid check
below handles the invalidity gate correctly.

First-submission behavior (submissionAttempts <= 1) is unchanged so
existing canSubmit semantics (e.g. onMount errors) are preserved.

Fixes TanStack#2034
@coderabbitai

coderabbitai Bot commented Apr 11, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fdf55fab-12d4-486e-bd7f-66c080186cbf

📥 Commits

Reviewing files that changed from the base of the PR and between 01ee70b and 5145dec.

📒 Files selected for processing (1)
  • .changeset/fix-stale-onblur-resubmit.md
✅ Files skipped from review due to trivial changes (1)
  • .changeset/fix-stale-onblur-resubmit.md

📝 Walkthrough

Walkthrough

Updated FormApi._handleSubmit to avoid skipping validation on subsequent submission attempts: when !this.state.canSubmit and not forced by _devtoolsSubmissionOverride, the method now returns early only for the first submission attempt. Later attempts continue to re-run field-level validators so stale onBlur errors can be cleared before deciding submission outcome.

Changes

Cohort / File(s) Summary
FormApi Submission Logic
packages/form-core/src/FormApi.ts
Changed _handleSubmit early-exit behavior: only the first invalid submit returns early and calls onSubmitInvalid; subsequent submits proceed to re-run field validators and then determine whether to call onSubmit or onSubmitInvalid.
FormApi Test Coverage
packages/form-core/tests/FormApi.spec.ts
Added regression test that verifies onBlur field validators re-run on re-submission and that stale onBlur errors are cleared when field conditions change.
Release Notes / Changeset
.changeset/fix-stale-onblur-resubmit.md
Added a changeset entry describing the patch: "fix(form-core): clear stale onBlur errors on re-submission".

Sequence Diagram(s)

mermaid
sequenceDiagram
participant User
participant FormApi
participant FieldValidator
participant SubmitHandlers
User->>FormApi: trigger handleSubmit()
FormApi->>FormApi: check state.canSubmit and _devtoolsSubmissionOverride
alt first submission and can't submit
FormApi->>SubmitHandlers: call onSubmitInvalid()
SubmitHandlers-->>User: notify invalid
else subsequent submission or can submit
FormApi->>FieldValidator: validateAllFields('submit')
FieldValidator-->>FormApi: update field errors (clears stale onBlur errors)
FormApi->>FormApi: isFieldsValid()
alt valid
FormApi->>SubmitHandlers: call onSubmit()
SubmitHandlers-->>User: submit success
else invalid
FormApi->>SubmitHandlers: call onSubmitInvalid()
SubmitHandlers-->>User: notify invalid
end
end

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped upon a submit and learned to try again,
Old blur-born errors loosened from their hold,
Validators woke and cleared the stain,
The form at last could do what’s bold. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing stale onBlur errors being cleared on re-submission, which directly addresses the core issue.
Description check ✅ Passed The description follows the template structure with all required sections completed: Changes section explains the fix and issue reference, Checklist items are checked, and Release Impact is properly documented with changeset confirmation.
Linked Issues check ✅ Passed The PR directly addresses issue #2034 by implementing field-level validation re-runs on re-submission to clear stale onBlur errors, matching the expected behavior described in the issue.
Out of Scope Changes check ✅ Passed All changes are in-scope: FormApi.ts modification fixes the submission logic, FormApi.spec.ts adds a regression test, and the changeset documents the fix—all directly related to resolving issue #2034.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@etodanik

Copy link
Copy Markdown

For context: This is actually fixing a nasty bug that cost us an App Review submission cycle. Would love to see a maintainer take a look.

@nx-cloud

nx-cloud Bot commented Jul 9, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit bdd4fc0

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 4m 21s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 41s View ↗

☁️ Nx Cloud last updated this comment at 2026-07-09 09:26:47 UTC

@pkg-pr-new

pkg-pr-new Bot commented Jul 9, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@2120

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@2120

@tanstack/form-devtools

npm i https://pkg.pr.new/@tanstack/form-devtools@2120

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@2120

@tanstack/preact-form

npm i https://pkg.pr.new/@tanstack/preact-form@2120

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@2120

@tanstack/react-form-devtools

npm i https://pkg.pr.new/@tanstack/react-form-devtools@2120

@tanstack/react-form-nextjs

npm i https://pkg.pr.new/@tanstack/react-form-nextjs@2120

@tanstack/react-form-remix

npm i https://pkg.pr.new/@tanstack/react-form-remix@2120

@tanstack/react-form-start

npm i https://pkg.pr.new/@tanstack/react-form-start@2120

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@2120

@tanstack/solid-form-devtools

npm i https://pkg.pr.new/@tanstack/solid-form-devtools@2120

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@2120

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@2120

commit: bdd4fc0

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.71%. Comparing base (6892ed0) to head (bdd4fc0).
⚠️ Report is 235 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2120      +/-   ##
==========================================
- Coverage   90.35%   89.71%   -0.64%     
==========================================
  Files          38       65      +27     
  Lines        1752     3200    +1448     
  Branches      444      804     +360     
==========================================
+ Hits         1583     2871    +1288     
- Misses        149      295     +146     
- Partials       20       34      +14     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@crutchcorn
crutchcorn merged commit 741da6b into TanStack:main Jul 9, 2026
9 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Field-level validation does not run on submit.

4 participants